<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * User: mmfffzzzz
 * Date: 14-3-28
 * Time: 下午12:14
 */


class Vote extends Base_Controller {


    public function __construct()
    {

        parent::__construct();
        $this->load->model('sys_user_model');
        //$this->load->model('sys_user_group_model');

    }

	public function index()
	{
		$_like = array();
        $_where = array();
        $_order_by = array('vote_id' => 'desc');

        $title = $this->input->get_post('title');

        if(notBlank($title)){
            $_like['title'] = $title;
        }
        $this->sys_user_model->_table="xq_vote";
        $page = $this->sys_user_model->getListByPage($this->page_offset, $_where, $_like, $_order_by,
        array("*"),
            array(
            ));
        foreach($page['list'] as $k => $v){
            if($v['start_time'] <= time()){
                $page['list'][$k]['no_edit']=1;
            }
        }
        $this->put("pagerbar", $this->getPageBar('index.php', $page['total']));
        $this->put("list", $page['list']);
        $this->put("title", $title);

		$this->render('vote_list.html');
	}

    public function add()
    {
        if(IS_POST){
            $options=$this->input->post("options");
            $options=array_unique(array_filter($options));
            $_data = $this->parseData(array('title','image','content','state','start_time',"end_time"));
            $_data['create_time']=date("Y-m-d H:i:s",time());
            if(!empty($_data['image'])){
                $img_arr=explode(".",$_data['image']);
                $suffix=$img_arr['1'];
                $time=time();
                $new_name='resources/images/'.$time.'.'.$suffix;
                move_uploaded_file($_FILES['pic']['tmp_name'],$new_name);
                $_data['image']=$new_name;
            }
            $_data['state']=empty($_data['state'])?'2':$_data['state'];
            $_data['start_time']=strtotime($_data['start_time']);
            $_data['end_time']=strtotime($_data['end_time']);
            //print_r($_data);exit;
        	$this->sys_user_model->_table="xq_vote";
            $this->sys_user_model->addEntity($_data);
            if($options){
                $this->sys_user_model->_table="xq_vote";
                $arr=$this->sys_user_model->getEntity(array(),array("max(vote_id)"));
                foreach($options as $v){
                    $data=array("vote_id"=>$arr['max(vote_id)'],"content"=>$v);
                    $this->sys_user_model->_table="xq_options";
                    $this->sys_user_model->addEntity($data);
                }
            }
            $this->handleResult(true);
        }
        //$this->put("group_list",$this->sys_user_group_model->getSysGroupArray());
        $this->render('vote_edit.html');
    }
    public function edit()
    {
        $_id = $this->input->get('id');
        
        if(IS_POST){
            $options=$this->input->post("options");
            $options=array_unique(array_filter($options));
            $_data = $this->parseData(array('title','image','content','state','start_time',"end_time"));
            $_data['create_time']=date("Y-m-d H:i:s",time());
            if(!empty($_FILES['pic']["tmp_name"]) && !empty($_data['image'])){
                $img_arr=explode(".",$_data['image']);
                $suffix=$img_arr['1'];
                $time=time();
                $new_name='resources/images/'.$time.'.'.$suffix;
                move_uploaded_file($_FILES['pic']['tmp_name'],$new_name);
                $_data['image']=$new_name;
            }
            $_data['state']=empty($_data['state'])?'2':$_data['state'];
            $_data['start_time']=strtotime($_data['start_time']);
            $_data['end_time']=strtotime($_data['end_time']);
        	$this->sys_user_model->_table="xq_vote";
            $this->sys_user_model->updateEntityByID($_data,$_id,"vote_id");
            $this->sys_user_model->_table="xq_options";
            $this->sys_user_model->deleteEntityByID($_id,"vote_id");
            if($options){
                $this->sys_user_model->_table="xq_vote";
                $arr=$this->sys_user_model->getEntity(array(),array("max(vote_id)"));
                foreach($options as $v){
                    $data=array("vote_id"=>$arr['max(vote_id)'],"content"=>$v);
                    $this->sys_user_model->_table="xq_options";
                    $this->sys_user_model->addEntity($data);
                }
            }
            $this->handleResult(true);
        }
        $this->sys_user_model->_table="xq_options";
        $list_options=$this->sys_user_model->getList(array("vote_id"=>$_id),array("*"));
        $this->sys_user_model->_table="xq_vote";
        $list=$this->sys_user_model->getEntityByID($_id,array("*"),"vote_id");
        $list['start_time']=date("Y-m-d H:i:s",$list['start_time']);
        $list['end_time']=date("Y-m-d H:i:s",$list['end_time']);
        $this->put('entity_options',$list_options);
        $this->put('entity',$list);
        $this->render('vote_edit.html');
    }
    public function view()
    {
        $_id = $this->input->get('id');
        $this->sys_user_model->_table="xq_vote_log";
        $list_logs=$this->sys_user_model->getList(array("vote_id"=>$_id),array("*"));
        foreach($list_logs as $v){
            $count_log[$v['options_id']]+=1;
        }
        $this->sys_user_model->_table="xq_options";
        $list_options=$this->sys_user_model->getList(array("vote_id"=>$_id),array("*"));
        foreach($list_options as $k => $v){
            $list_options[$k]['count']=empty($count_log[$v['op_id']])?"0":$count_log[$v['op_id']];
        }
        $this->sys_user_model->_table="xq_vote";
        $list=$this->sys_user_model->getEntityByID($_id,array("*"),"vote_id");
        $list['start_time']=date("Y-m-d H:i:s",$list['start_time']);
        $list['end_time']=date("Y-m-d H:i:s",$list['end_time']);
        $this->put('entity_options',$list_options);
        $this->put('entity',$list);
        $this->put('count_log',$count_log);
        $this->render('vote_view.html');
    }
    public function delete()
    {
        $_id = $this->input->get('id');
        $this->sys_user_model->_table="xq_vote";
        echo $this->sys_user_model->deleteEntityByID($_id,"vote_id")?STATUS_SUCCESS:STATUS_ERROR;
    }

    public function status(){
        $_id = $this->input->get('id');
        $_state = $this->input->get('state');
        $_data['state'] = $_state;
        $this->sys_user_model->_table="xq_vote";
        echo $this->sys_user_model->updateEntityByID($_data,$_id,"vote_id")?STATUS_SUCCESS:STATUS_ERROR;
    }

}

